package cn.stylefeng.guns.modular.mobile.base;

import cn.stylefeng.guns.modular.manager.entity.FlowBdMember;
import cn.stylefeng.guns.modular.manager.entity.FlowOrder;
import cn.stylefeng.guns.modular.manager.service.FlowBdMemberService;
import cn.stylefeng.guns.sys.modular.system.entity.Dept;
import cn.stylefeng.guns.sys.modular.system.entity.FlowDeptPhones;
import cn.stylefeng.guns.sys.modular.system.service.DeptService;
import cn.stylefeng.guns.sys.modular.system.service.FlowDeptPhonesService;
import cn.stylefeng.guns.utils.AliyuncsUtil;
import cn.stylefeng.guns.utils.AppUtil;
import cn.stylefeng.roses.core.util.MD5Util;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.ModelAttribute;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Random;

public class MobileBaiduBaseController {
    @Autowired
    private FlowBdMemberService flowBdMemberService;

    protected FlowBdMember bdMember;

    //登录数据
    @ModelAttribute
    public void init(HttpServletRequest request) throws Exception{
        String openid = request.getHeader("openid");
        String timestamp = request.getHeader("timestamp");
        String code = request.getHeader("code");
        if(code == null){
            throw new Exception("参数有误01");
        }
        if(timestamp == null){
            throw new Exception("参数有误02");
        }
        if(code == null){
            throw new Exception("参数有误03");
        }
        long time = Long.parseLong(timestamp);
        Long s = (System.currentTimeMillis() - time) / (1000 * 60);
        if(s > 30){
            throw new Exception("登录时间已过期，请重新登录.");
        }

        String ncode = MD5Util.encrypt(openid + AppUtil.key + timestamp);
        if(!ncode.toLowerCase().equals(code.toLowerCase())){
            throw new Exception("校验失败，请重新登录.");
        }
        //查询用户值
        QueryWrapper<FlowBdMember> shopMemberQueryWrapper = new QueryWrapper<>();
        shopMemberQueryWrapper.eq("bd_openid",openid);
        FlowBdMember bdMember = flowBdMemberService.getOne(shopMemberQueryWrapper);
        this.bdMember = bdMember;

        if(this.bdMember == null){
            throw new Exception("不存在该用户");
        }
    }

    //发送生成订单的短信
    @Autowired
    protected FlowDeptPhonesService flowDeptPhonesService;
    @Autowired
    protected DeptService deptService;

    /**
     * 根据订单查询发送的短信
     * @param flowOrder
     */
    protected void sendMsg(FlowOrder flowOrder){
        //查询服务的网点
        try{
            if(flowOrder.getStoreId() == null || flowOrder.getStoreId().intValue() <= 0){
                //查询总公司网点
                QueryWrapper<Dept> deptQueryWrapper = new QueryWrapper<>();
                deptQueryWrapper.eq("pid",0);
                deptQueryWrapper.last("limit 1");
                Dept dept = deptService.getOne(deptQueryWrapper);
                if(dept != null){
                    //查询该客服人员
                    QueryWrapper<FlowDeptPhones> flowDeptPhonesQueryWrapper = new QueryWrapper<>();
                    flowDeptPhonesQueryWrapper.eq("dept_id",dept.getDeptId());
                    List<FlowDeptPhones> flowDeptPhones = flowDeptPhonesService.list(flowDeptPhonesQueryWrapper);
                    if(flowDeptPhones.size() > 0){
                        Random random = new Random();
                        int n = random.nextInt(flowDeptPhones.size());
                        AliyuncsUtil.sendSms(1,flowDeptPhones.get(n).getMobile(),flowOrder.getName(),flowOrder.getPhone());
                    }
                }
            }else{
                //查询管理网点的客服电话
                QueryWrapper<FlowDeptPhones> flowDeptPhonesQueryWrapper = new QueryWrapper<>();
                flowDeptPhonesQueryWrapper.apply("dept_id in (select dept_id from flow_dept_store where store_id = "+flowOrder.getStoreId().longValue()+")");
                List<FlowDeptPhones> flowDeptPhones = flowDeptPhonesService.list(flowDeptPhonesQueryWrapper);
                if(flowDeptPhones.size() > 0){
                    Random random = new Random();
                    int n = random.nextInt(flowDeptPhones.size());
                    AliyuncsUtil.sendSms(1,flowDeptPhones.get(n).getMobile(),flowOrder.getName(),flowOrder.getPhone());
                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}
